package com.changgou.goods.controller;

import com.changgou.goods.pojo.Category;
import com.changgou.goods.service.CategoryService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2021/11/30 4:08 下午
 * @Version 1.0
 * @description 商品分类管理
 */
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据parentId查询子菜单
     * @param pId
     * @return
     */
    @PostMapping(value = "/pid")
    public Result<List<Category>> findByParentId(@PathVariable Integer pId) {
        List<Category> list = categoryService.findListById(pId);
        return new Result<List<Category>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 新增商品分类
     * @param category
     * @return
     */
    @PostMapping(value = "/add")
    public Result add(@RequestBody Category category) {
        int add = categoryService.add(category);
        return new Result(true, StatusCode.OK, "新增成功", add);
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        int delete = categoryService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功", delete);
    }

    /**
     * 更新商品分类
     * @param category
     * @param id
     * @return
     */
    @PutMapping(value = "/update")
    public Result update(@RequestBody Category category, @PathVariable Integer id) {
        categoryService.update(category, id);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    /**
     * 根据id查询商品分类
     * @param id
     * @return
     */
    @PostMapping(value = "/find/{id}")
    public Result<Category> findById(@PathVariable Integer id) {
        Category category = categoryService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", category);
    }

    /**
     * 查询所有商品分类
     * @return
     */
    @PostMapping(value = "/findAll")
    public Result<List<Category>> findAll() {
        List<Category> categoryList = categoryService.findAll();
        return new Result<List<Category>>(true, StatusCode.OK, "查询成功", categoryList);
    }

    /**
     * 多条件查询商品分类
     * @param category
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Category>> findList(@RequestBody(required = false) Category category) {
        List<Category> categoryList = categoryService.findList(category);
        return new Result<List<Category>>(true, StatusCode.OK, "查询成功", categoryList);
    }

    /**
     * 分页查询商品分类
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping(value = "/search/{pageSize}/{pageNum}")
    public Result<List<Category>> findPage(@PathVariable Integer pageSize, @PathVariable Integer pageNum) {
        PageInfo<Category> pageInfo = categoryService.findPage(pageSize, pageNum);
        return new Result<List<Category>>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 多条件分页查询商品分类
     * @param category
     * @param pageSize
     * @param pageNum
     * @return
     */
    @PostMapping(value = "/search/{pageSzie}/{pageNum}")
    public Result<List<Category>> findPageList(@RequestBody(required = false) Category category, @PathVariable Integer pageSize, @PathVariable Integer pageNum) {
        PageInfo<Category> pageList = categoryService.findPageList(category, pageSize, pageNum);
        return new Result<List<Category>>(true, StatusCode.OK, "查询成功", pageList);
    }

}
